﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HCFramework.API.LINQ;

namespace HCFramework.Product.FrontEndControl
{
    public partial class CategoryMenu : System.Web.UI.UserControl
    {
        public string UrlContinue
        {
            get
            {
                return litUrl.Text;
            }
            set
            {
                litUrl.Text = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (UrlContinue == "") UrlContinue = M.Common.GetRootAddress() + "/Product/Default.aspx";
                BindData();
                HightLightPath();
            }
        }

        private void HightLightPath()
        {
            
        }

        private void BindData()
        {
            List<ProductCategory> listMenuGroup = C.ProductCategoryController.ListCategoryCache.Where(ik => ik.IsActive && ik.ParentID == null).OrderBy(ik => ik.OrderDisplay).ToList();
            foreach (ProductCategory group in listMenuGroup)
            {
                string tem = M.FileM.Read(@"\Product\FrontEndControl\Template\CategoryMenuGroup.htm");
                tem = tem.Replace("{text}", M.Common.GetLang(group.Name));
                tem = tem.Replace("{categoryID}", group.ProductCategoryID + "");
                tem = tem.Replace("{items}", GetDisplayItem(group));
                litCategoryMenuGroups.Text += tem;
            }
        }

        private string GetDisplayItem(ProductCategory group)
        {
            string ret = "";
            foreach (ProductCategory item in group.ProductCategories.ToList())
            {
                string tem = M.FileM.Read(@"\Product\FrontEndControl\Template\CategoryMenuItem.htm");
                tem = tem.Replace("{text}", M.Common.GetLang(item.Name));
                tem = tem.Replace("{url}", M.Http.SetPara(UrlContinue, "categoryID", item.ProductCategoryID.ToString()));
                ret += tem;
            }
            return ret;
        }

        public string GetRootCategoryID()
        {
            if (M.UrlQuery.categoryID.HasValue)
            {
                return C.ProductCategoryController.GetRootCategory(M.UrlQuery.categoryID.Value)
                    .ProductCategoryID.ToString();
            }
            else
            {
                return "";
            }
        }
    }
}